{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Exercise -Version Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- This is the exercise based on the forked repository on GitHub\n",
    "-  Help to improve it if you want, just add the solution under the question\n",
    "- Each notebook only contains 10 questions which is convenient for everyone to locate each question to modify, you can add your own name under the author's list below, let's contribute together !\n",
    "- To visit my personal site: [Click Here](http://yaozeliang.com/resume)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 61\n",
    "\n",
    "\n",
    ">***The Fibonacci Sequence is computed based on the following formula:***\n",
    "```\n",
    "f(n)=0 if n=0\n",
    "f(n)=1 if n=1\n",
    "f(n)=f(n-1)+f(n-2) if n>1\n",
    "```\n",
    ">***Please write a program to compute the value of f(n) with a given n input by console.***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "7\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "13\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    "> ***We can define recursive function in Python.***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(n):\n",
    "    if n==0:\n",
    "        return 0\n",
    "    elif n==1:\n",
    "        return 1\n",
    "    else:\n",
    "        return f(n-1)+f(n-2)\n",
    "f(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 62\n",
    "\n",
    ">***The Fibonacci Sequence is computed based on the following formula:***\n",
    "```\n",
    "f(n)=0 if n=0\n",
    "f(n)=1 if n=1\n",
    "f(n)=f(n-1)+f(n-2) if n>1\n",
    "```\n",
    ">***Please write a program to compute the value of f(n) with a given n input by console.***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "7\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "0,1,1,2,3,5,8,13\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0,1,1,2,3,5,8,13'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(n):\n",
    "    if n==0:\n",
    "        return 0\n",
    "    elif n==1:\n",
    "        return 1\n",
    "    else:\n",
    "        return f(n-1)+f(n-2)\n",
    "values=[str(f(x)) for x in range(0,8)]\n",
    "values=\",\".join(values) \n",
    "values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 63\n",
    "\n",
    ">***Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "10\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "0,2,4,6,8,10\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    "> ***Use yield to produce the next value in generator.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "def gen_even(n):\n",
    "    for i in range(n+1):\n",
    "        if i%2==0:\n",
    "            yield i\n",
    "n=int(input())\n",
    "genrator=gen_even(n)\n",
    "# for i in range(n+1):\n",
    "#     print(next(genrator))\n",
    "\n",
    "for i in genrator:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 64\n",
    "\n",
    ">***Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "100\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "0,35,70\n",
    "```\n",
    ">***In case of input data being supplied to the question, it should be assumed to be a console input.***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "35\n",
      "70\n"
     ]
    }
   ],
   "source": [
    "def gen_division57(n):\n",
    "    for i in range(n+1):\n",
    "        if i%5==0 and i%7==0:\n",
    "            yield i\n",
    "generator=gen_division57(100)\n",
    "for i in generator:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 65\n",
    ">***Please write assert statements to verify that every number in the list [2,4,6,8] is even.***\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "存在奇数",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1996/185559581.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m%\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"存在奇数\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m: 存在奇数"
     ]
    }
   ],
   "source": [
    "value=[2,3,6,8]\n",
    "for i in value:\n",
    "    assert(i%2==0),\"存在奇数\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 66\n",
    "\n",
    ">***Please write a program which accepts basic mathematic expression from console and print the evaluation result.***\n",
    "\n",
    ">***Example:\n",
    "If the following n is given as input to the program:***\n",
    "```\n",
    "35 + 3\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "38\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value=\"35.0+3\"\n",
    "result=eval(value)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 67\n",
    "\n",
    "\n",
    ">***Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "def bin_search(li,target):\n",
    "    top=li[-1]\n",
    "    btm=li[0]\n",
    "    index=-1\n",
    "    while(top>=btm):\n",
    "        mid=(top+btm)//2\n",
    "        if li[mid]==target:\n",
    "            index=mid\n",
    "            break\n",
    "        elif li[mid]<target:\n",
    "            btm=mid+1\n",
    "        elif li[mid]>target:\n",
    "            top=mid-1\n",
    "    return index\n",
    "li=[i for i in range(0,100,2)]\n",
    "print(li)\n",
    "result=bin_search(li,4)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Python code to demonstrate working of binary search in library \n",
    "from bisect import bisect_right \n",
    "\n",
    "def BinarySearch(a, x): \n",
    "    i = bisect_right(a, x) \n",
    "    if i != len(a)+1 and a[i-1] == x: \n",
    "        return (i-1) \n",
    "    else: \n",
    "        return -1\n",
    "\n",
    "lst = [1,2,4,5,6,7,8] \n",
    "x = int(input()) \n",
    "res = BinarySearch(lst, x) \n",
    "if res == -1: \n",
    "    print(x, \"is absent\") \n",
    "else: \n",
    "    print(\"Last occurrence of\", x, \"is present at\", res) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 68\n",
    "\n",
    ">***Please generate a random float where the value is between 10 and 100 using Python module.***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[65.08640334 51.40357155 14.14330724 10.00595458 30.63369762 57.6836044\n",
      " 77.52129839 99.88767785 61.64173554 83.36748683]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(100*np.random.random(10)+10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12.274365990253454"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "random.uniform(10,100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 69\n",
    ">***Please generate a random float where the value is between 5 and 95 using Python module.***\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "75.38361097120053"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "random.uniform(5,95)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 70\n",
    "\n",
    ">***Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 8, 4, 4, 4]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "value=[int(random.uniform(0,10)) for i in range(10)]\n",
    "value=list(filter(lambda x:x%2==0,value))\n",
    "value"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
